package com.nus.fellowmark.modules.rbac.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.common.utils.ClassUtil;
import com.nus.fellowmark.dao.SysMenuDao;
import com.nus.fellowmark.dao.SysRoleDao;
import com.nus.fellowmark.dao.SysUserRoleMappingDao;
import com.nus.fellowmark.entity.SysMenuEntity;
import com.nus.fellowmark.entity.SysRoleEntity;
import com.nus.fellowmark.entity.SysUserRoleMappingEntity;
import com.nus.fellowmark.modules.rbac.enums.RoleEnum;
import com.nus.fellowmark.modules.rbac.vo.SysMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;

import com.nus.fellowmark.dao.SysRoleMenuMappingDao;
import com.nus.fellowmark.entity.SysRoleMenuMappingEntity;
import com.nus.fellowmark.modules.rbac.service.SysRoleMenuMappingService;

import static java.util.stream.Collectors.toList;

/**
* @author Ruoyi Chen
* @email  chenruoyisz@gmail.com
* @create 2023-02-11
*/
@Service("sysRoleMenuMappingService")
public class SysRoleMenuMappingServiceImpl extends ServiceImpl<SysRoleMenuMappingDao, SysRoleMenuMappingEntity> implements SysRoleMenuMappingService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysMenuDao sysMenuDao;
    
    @Autowired
    private SysUserRoleMappingDao sysUserRoleMappingDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleMenuMappingEntity> page = this.page(
                new Query<SysRoleMenuMappingEntity>().getPage(params),
                new QueryWrapper<SysRoleMenuMappingEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysMenuEntity> getMenuOrPermissionList(Integer userId, String userType) {
        if (userId == null) {
            return null;
        }

        return sysMenuDao.getMenuOrPermissionList(userId, userType);
    }

    @Override
    public List<SysRoleEntity> getRoleList(Integer userId) {
        return sysRoleDao.getRoleList(userId);
    }


    @Override
    public ServerResponseEntity<List<SysMenuVO>> getMenuList() {
        List<SysMenuEntity> menus = sysMenuDao.getMenuListByRoleId(getRoleId());
        return ServerResponseEntity.success(ClassUtil.copyList(menus, SysMenuVO.class));
    }

    @Override
    public ServerResponseEntity<List<String>> getPermissionCodeList() {
        List<SysMenuEntity> menus = sysMenuDao.getPermissionCodeByRoleId(getRoleId());
        List<String> permissionCode = menus.stream().map(SysMenuEntity::getCode).collect(toList());
        return ServerResponseEntity.success(permissionCode);
    }

    @Override
    public int saveUserRole(SysUserRoleMappingEntity mapping) {
        return sysUserRoleMappingDao.insert(mapping);
    }

    @Override
    public SysUserRoleMappingEntity selectUserRoleMapping(Integer userId) {
        return sysUserRoleMappingDao.selectByUserId(userId);
    }

    @Override
    public SysUserRoleMappingEntity getUserRoleMappingByUserId(Integer userId) {
        return null;
    }

    @Override
    public int updateUserRole(SysUserRoleMappingEntity mapping) {
        return sysUserRoleMappingDao.updateById(mapping);
    }


    /**
     * 获取当前角色id
     *
     * @return
     */
    private int getRoleId() {
        boolean flag = StpUtil.isLogin();
        int roleId = RoleEnum.NO_LOGIN.getType();
        if (flag) {
            // if the user is visitor
            if (StpUtil.hasRole(RoleEnum.VISITOR.getCode())) {
                roleId = RoleEnum.VISITOR.getType();
            }
            // if the user is auth user
            if (StpUtil.hasRole(RoleEnum.AUTH.getCode())) {
                roleId = RoleEnum.AUTH.getType();
            }
        }
        return roleId;
    }

}
